package com.tcm.pay.request;

import com.tcm.pay.common.HttpRequest;
import com.tcm.pay.domain.dto.MerchantInDTO;
import com.tcm.pay.domain.dto.QueryMerchantInDTO;
import com.tcm.pay.util.Base64Util;
import com.tcm.pay.util.RandomUtil;
import org.springframework.stereotype.Component;

@Component
public class MerchantIn {

    public static String MERCHANTINURL = "https://gwapi.yemadai.com/scanpay/merchantIn";

    public static String MERCHANTQUERYURL = "https://gwapi.yemadai.com/scanpay/merchantInQuery";

    /**
     * 支付宝入驻
     * @param merchantInDTO
     * @return
     */
    public String zfb(MerchantInDTO merchantInDTO) {
        merchantInDTO.setMerNo("50829");
        merchantInDTO.setVersion("1.0");
        merchantInDTO.setPayType("ZFBZF");
        merchantInDTO.setRandomStr(RandomUtil.getStringRandom(10));

        merchantInDTO.setChannelNo("2088431915915352");//微信渠道号/支付宝PID   联系汇潮商务
        merchantInDTO.setMerName("上海泽体科技有限公司");//商户名称
        merchantInDTO.setShortName("上海泽体科技");
        merchantInDTO.setContactName("吴姜兵");
        merchantInDTO.setServicePhone("13818631238");

        merchantInDTO.setBusiness("2016062900190296");//医疗健康
        merchantInDTO.setMcc("5122");//医疗健康

        merchantInDTO.setContactTag("06");
        merchantInDTO.setContactType("LEGAL_PERSON");
        merchantInDTO.setProvince("上海市");
        merchantInDTO.setCity("上海市");
        merchantInDTO.setDistrict("奉贤区");
        merchantInDTO.setAddress("南桥镇南桥路337号一幢");
        merchantInDTO.setCardNo("b232b10110002721b21");
        merchantInDTO.setCardName("上海泽体科技有限公司");
        merchantInDTO.setSignInfo(HttpRequest.makeSignForMerchantIn(merchantInDTO.getMerNo(),merchantInDTO.getPayType(),merchantInDTO.getRandomStr()));

        StringBuffer xml = new StringBuffer();
        xml.append("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>");
        xml.append("<ScanMerchantInRequest>");
        xml.append("<MerNo>").append(merchantInDTO.getMerNo()).append("</MerNo>");
        xml.append("<Version>").append(merchantInDTO.getVersion()).append("</Version>");
        xml.append("<PayType>").append(merchantInDTO.getPayType()).append("</PayType>");
        xml.append("<SignInfo>").append(merchantInDTO.getSignInfo()).append("</SignInfo>");
        xml.append("<RandomStr>").append(merchantInDTO.getRandomStr()).append("</RandomStr>");
        xml.append("<ChannelNo>").append(merchantInDTO.getChannelNo()).append("</ChannelNo>");
        xml.append("<MerchantInfo>");
        xml.append("<MerName>").append(merchantInDTO.getMerName()).append("</MerName>");
        xml.append("<ShortName>").append(merchantInDTO.getShortName()).append("</ShortName>");
        xml.append("<ContactName>").append(merchantInDTO.getContactName()).append("</ContactName>");
        xml.append("<ServicePhone>").append(merchantInDTO.getServicePhone()).append("</ServicePhone>");
        xml.append("<Business>").append(merchantInDTO.getBusiness()).append("</Business>");
        xml.append("<Mcc>").append(merchantInDTO.getMcc()).append("</Mcc>");
        xml.append("<ContactTag>").append(merchantInDTO.getContactTag()).append("</ContactTag>");
        xml.append("<ContactType>").append(merchantInDTO.getContactType()).append("</ContactType>");
        xml.append("<City>").append(merchantInDTO.getCity()).append("</City>");
        xml.append("<District>").append(merchantInDTO.getDistrict()).append("</District>");
        xml.append("<Address>").append(merchantInDTO.getAddress()).append("</Address>");
        xml.append("<Province>").append(merchantInDTO.getProvince()).append("</Province>");
        xml.append("<CardNo>").append(merchantInDTO.getCardNo()).append("</CardNo>");
        xml.append("<CardName>").append(merchantInDTO.getCardName()).append("</CardName>");
        xml.append("</MerchantInfo>");
        xml.append("</ScanMerchantInRequest>");
        String result = HttpRequest.sendData(MERCHANTINURL, Base64Util.encode(xml.toString()), merchantInDTO.getRandomStr());
        return result;
    }

    /**
     * 微信入驻
     * @param merchantInDTO
     * @return
     */
    public String wx(MerchantInDTO merchantInDTO) {
        merchantInDTO.setMerNo("50829");
        merchantInDTO.setVersion("1.0");
        merchantInDTO.setPayType("WXZF_ONLINE");                             //微信线上
        merchantInDTO.setRandomStr(RandomUtil.getStringRandom(10));   //最大为32位

        merchantInDTO.setChannelNo("405896062");//微信渠道号/支付宝PID  联系汇潮商务

        merchantInDTO.setMerName("上海泽体科技有限公司");//要求与营业执照上的名称一致，如为个体、无营业执照的，则填写为：个体户***（如个体户张三）
        merchantInDTO.setShortName("上海泽体科技");//商户简称
        merchantInDTO.setContactName("吴姜兵");
        merchantInDTO.setServicePhone("13818631238");//客服电话

        merchantInDTO.setBusiness("758");//行业类别
        merchantInDTO.setProvince("上海市");
        merchantInDTO.setCity("上海市");
        merchantInDTO.setDistrict("奉贤区");
        merchantInDTO.setAddress("南桥镇南桥路337号一幢");
        //确保商户号 、支付方式 、随机字符串 不能为空，不然签名失败
        merchantInDTO.setSignInfo(HttpRequest.makeSignForMerchantIn(merchantInDTO.getMerNo(),merchantInDTO.getPayType(),merchantInDTO.getRandomStr()));

        StringBuffer xml = new StringBuffer();
        xml.append("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>");
        xml.append("<ScanMerchantInRequest>");
        xml.append("<MerNo>").append(merchantInDTO.getMerNo()).append("</MerNo>");
        xml.append("<Version>").append(merchantInDTO.getVersion()).append("</Version>");
        xml.append("<PayType>").append(merchantInDTO.getPayType()).append("</PayType>");
        xml.append("<SignInfo>").append(merchantInDTO.getSignInfo()).append("</SignInfo>");
        xml.append("<RandomStr>").append(merchantInDTO.getRandomStr()).append("</RandomStr>");
        xml.append("<ChannelNo>").append(merchantInDTO.getChannelNo()).append("</ChannelNo>");
        xml.append("<MerchantInfo>");
        xml.append("<MerName>").append(merchantInDTO.getMerName()).append("</MerName>");
        xml.append("<ShortName>").append(merchantInDTO.getShortName()).append("</ShortName>");
        xml.append("<ServicePhone>").append(merchantInDTO.getServicePhone()).append("</ServicePhone>");
        xml.append("<Business>").append(merchantInDTO.getBusiness()).append("</Business>");
        xml.append("<City>").append(merchantInDTO.getCity()).append("</City>");
        xml.append("<District>").append(merchantInDTO.getDistrict()).append("</District>");
        xml.append("<Address>").append(merchantInDTO.getAddress()).append("</Address>");
        xml.append("<Province>").append(merchantInDTO.getProvince()).append("</Province>");
        xml.append("</MerchantInfo>");
        xml.append("</ScanMerchantInRequest>");
        String result = HttpRequest.sendData(MERCHANTINURL, Base64Util.encode(xml.toString()), merchantInDTO.getRandomStr());
        return result;
    }

    /**
     * 渠道商户入驻查询
     * @param queryMerchantInDTO
     * @return
     */
    public String queryMerchant(QueryMerchantInDTO queryMerchantInDTO){
        queryMerchantInDTO.setMerNo("50829");
        queryMerchantInDTO.setRandomStr(RandomUtil.getStringRandom(12));
        queryMerchantInDTO.setVersion("1.0");
        queryMerchantInDTO.setSignInfo(HttpRequest.makeSignForQueryMerchantIn(queryMerchantInDTO.getMerNo(), queryMerchantInDTO.getPayType(), queryMerchantInDTO.getCompanyNo(), queryMerchantInDTO.getRandomStr()));

        StringBuffer xml = new StringBuffer();
        xml.append("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>");
        xml.append("<ScanMerchantInQueryRequest>");
        xml.append("<MerNo>").append(queryMerchantInDTO.getMerNo()).append("</MerNo>");
        xml.append("<Version>").append(queryMerchantInDTO.getVersion()).append("</Version>");
        xml.append("<PayType>").append(queryMerchantInDTO.getPayType()).append("</PayType>");
        xml.append("<SignInfo>").append(queryMerchantInDTO.getSignInfo()).append("</SignInfo>");
        xml.append("<RandomStr>").append(queryMerchantInDTO.getRandomStr()).append("</RandomStr>");
        xml.append("<CompanyNo>").append(queryMerchantInDTO.getCompanyNo()).append("</CompanyNo>");
        xml.append("</ScanMerchantInQueryRequest>");

        String result = HttpRequest.sendData(MERCHANTQUERYURL, Base64Util.encode(xml.toString()), queryMerchantInDTO.getRandomStr());
        return result;
    }

}
